package vue;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import modele.Utilisateur;

import controleur.Controleur;

public class PopupInscription extends JFrame implements ActionListener{
	
	private FenetrePrincipale ihm;
	private JButton valider;
	private JTextField pseudo;
	private JPasswordField mdp1, mdp2;
	private JLabel info;
	
	public PopupInscription(FenetrePrincipale ihm) {

		this.ihm = ihm;
		setTitle("S'inscrire");
		setLocation(100, 100);
		setSize(300, 250);
		
		JPanel panelGeneral = new JPanel(new GridLayout(5,1));
		
		JPanel panel = new JPanel(new GridLayout(1,2));
		panel.add(new JLabel("Pseudo :"));
		pseudo = new JTextField();
		panel.add(pseudo);
		panelGeneral.add(panel);
		
		panel = new JPanel(new GridLayout(1,2));
		panel.add(new JLabel("Mdp :"));
		mdp1 = new JPasswordField();
		panel.add(mdp1);
		panelGeneral.add(panel);
		
		panel = new JPanel(new GridLayout(1,2));
		panel.add(new JLabel("Confirmation :"));
		mdp2 = new JPasswordField();
		panel.add(mdp2);
		panelGeneral.add(panel);
		
		valider = new JButton("Valider");
		valider.addActionListener(this);
		panelGeneral.add(valider);
		
		info = new JLabel();
		panelGeneral.add(info);
		this.add(panelGeneral);
		
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == valider) {
			if(ihm.getControleur().pseudoExiste(pseudo.getText())) {
				info.setText("Ce pseudo est déjà prit.");
			} else if(!mdp1.getText().equals(mdp2.getText())) {
				info.setText("Les mots de passes ne correspondent pas.");
			} else if(mdp1.getText().equals(mdp2.getText())) {
				info.setText("Inscription réussie.");
				Utilisateur newUser = new Utilisateur(pseudo.getText(), mdp1.getText(), 0);
				ihm.getControleur().ajouterUtilisateur(newUser);
			}
		}
	}
	
	
}
